package com.mz.mattress.client.api.biz;

import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.jwt.ClientJwt;
import com.mz.mattress.api.resp.UserAuth;
import com.mz.mattress.api.util.IpUtil;
import com.mz.mattress.api.util.JacksonUtils;
import com.mz.mattress.client.api.req.WxLoginReq;
import com.mz.mattress.client.api.resp.RegisterResp;
import com.mz.mattress.client.common.Holder;
import com.mz.mattress.client.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 鉴权服务
 */
@RestController
@RequestMapping("/auth")
@Validated
@Slf4j
public class AuthController {

    @Autowired
    private MemberService memberService;

    //微信登录，注册用户微信信息
    @PostMapping("wxLogin")
    public Result<UserAuth> wxLogin(@RequestBody WxLoginReq wxLoginReq, HttpServletRequest request) {
        log.info("WxLogin_req_params={}", JacksonUtils.toJson(wxLoginReq));
        String ip = IpUtil.client(request);
        return memberService.WxLogin(wxLoginReq, ip);
    }

    @PostMapping("bindWxMobile")
    public Result<RegisterResp> bindWxMobile(@RequestBody String body) {
        // DONE 注册微信手机号
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        String sessionKey = clientJwt.getSessionKey();
        Long memberId = clientJwt.getMemberId();
        return memberService.bindWxMobile(sessionKey, body, memberId);
    }

    @RequestMapping("checkToken")
    public Result<String> checkToken() {
        return Result.ok();
    }

    @RequestMapping("reLogin")
    public Result<UserAuth> reLogin() {
        return memberService.reLogin();
    }

}
